package io.babystar.modules.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.babystar.modules.business.dao.CourseDateDao;
import io.babystar.modules.business.dao.ShopDao;
import io.babystar.modules.business.entity.CourseDateEntity;
import io.babystar.modules.business.entity.ShopEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.Query;

import io.babystar.modules.business.dao.CourseDao;
import io.babystar.modules.business.entity.CourseEntity;
import io.babystar.modules.business.service.CourseService;


@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseDao, CourseEntity> implements CourseService {

    @Autowired
    private CourseDao courseDao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private CourseDateDao courseDateDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CourseEntity> page = this.page(
                new Query<CourseEntity>().getPage(params),
                new QueryWrapper<CourseEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 重写分页
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageCourse(Map<String, Object> params) {
        Long organizeId = (Long)params.get("organizeid");
        List<Long> shops = new ArrayList<>();
        if(organizeId!=null){
            Map<String,Object> p = new HashMap<>();
            p.put("business_id",organizeId);
            List<ShopEntity> list = shopDao.selectByMap(p);
            for(ShopEntity shop:list){
                shops.add(shop.getId());
            }
        }
        Page<CourseEntity> page = new Page<>();
        Integer count = courseDao.selectCount( new QueryWrapper<CourseEntity>()
                .eq(!"".equals(params.get("shopId"))&& params.get("shopId") != null, "shop_id", params.get("shopId"))
                .in("shop_id",shops)
        );
        page.setTotal(count);
        Integer pageNuber = Integer.parseInt(params.get("page").toString());
        Integer pageSize = Integer.parseInt(params.get("limit").toString());
        page.setCurrent(pageNuber);
        page.setSize(pageSize);
        Long current = page.getCurrent();
        Long size = page.getSize();
        Long currentStart = size*current - size;
        params.put("currentStart",currentStart);
        params.put("size",size);
        params.put("organizeId",organizeId);
        page.setRecords(courseDao.queryPageCourse(params));

        return new PageUtils(page);
    }

    /**
     *校验已被选课的课程不可进行修改和删除操作
     * @param courseId
     * @return
     */
    @Override
    public Integer qureyCourseInfo(Integer courseId){
        return courseDao.qureyCourseInfo(courseId);
    }




}